package it.eng.area118.mdogpscollector.fragment;

import java.util.List;

import it.eng.area118.mdogpscollector.R;
import it.eng.area118.mdogpscollector.model.CheckPoint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class CheckPointAdapter extends ArrayAdapter<CheckPoint> {

	private int layoutResourceId;
	private Context context;
	private List<CheckPoint> objects;
	private int mItemIndex;

	public CheckPointAdapter(Context context, int layoutResourceId, int textViewResourceId, List<CheckPoint> objects) {
		super(context, layoutResourceId, textViewResourceId, objects);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.objects = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = super.getView(position, convertView, parent);
		// WeatherHolder holder = null;

		if (row == null) {
			CheckPoint cp = objects.get(position);
			if (cp.getLocation() != null) {
				LayoutInflater inflater = ((Activity) context).getLayoutInflater();
				// row = inflater.inflate(layoutResourceId, parent, false);
				// row.setBackgroundResource(R.drawable.border_fill);
			}
		} else {

			CheckPoint cp = objects.get(position);
			if (cp.getLocation() != null) {
				if (position == mItemIndex) {
					row.setSelected(true);
					row.setPressed(true);
					row.setBackgroundResource(R.drawable.border_fill_selected);
				} else {
					row.setBackgroundResource(R.drawable.border_fill);
				}
			} else {
				if (position == mItemIndex) {
					row.setSelected(true);
					row.setPressed(true);
					row.setBackgroundResource(R.drawable.border_transparent_selected);
				} else {
					row.setBackgroundResource(R.drawable.border_transparent);
				}
			}
		}

		return row;
	}

	public void setSelectItem(int index) {
		mItemIndex = index;
	}

	public int getSelectItem() {
		return mItemIndex;
	}

	public void setContent(List<CheckPoint> objects) {
		clear();
		addAll(objects);
		notifyDataSetChanged();
	}
}
